import router from './router'
import store from './store'
import api from '@/api'
import {
  Notify
} from 'vant'
import {
  filterAsyncRouter
} from '@/store/modules/configs'
import {
  LgetItem,
  LsetItem,
} from '@/utils/storage'
import {
  isIos,
  isWeiXin
} from '@/utils/index'
import {
  wxInit
} from '@/utils/jssdkInit'
import axios from "axios";

router.beforeEach((to, from, next) => {
  LsetItem('jwtToken','2f6289b5e602471e98bfcac075199bff')
  console.log(to, from)
  if (to.query.id) {
    LsetItem('voteId', to.query.id)
  }

  // if (from.path === '/' && isIos()) {
  //   wxInit()
  // }
  const jwtToken = LgetItem('jwtToken')
  if (jwtToken) {
    if (!store.state.configs.tabbar.length) {
      report()
      loadConfig(next, to)
    } else {
      next()
    }
  } else {
    if (isWeiXin()) {
      if (to.query.code) {
        api('sign.webLogin', {
            code: to.query.code,
            state: to.query.state
          }).then(res => {
            LsetItem('jwtToken', res)
            report()
            next({
              path: '/',
              replace: true
            })
          })
          .catch(err => {
            Notify({
              type: 'danger',
              message: err
            })
            console.log('web:jwtToken Err', err)
          })
      } else {
        if (to.query.id) {
          //// window.location.origin+window.location.pathname
          api('sign.webAuth', {
            voteId: to.query.id,
            redirectUri: window.location.href
          }).then(res => {

            window.location.replace(res)
          })
        }
      }
    } else {
      api('sign.temporaryLogin').then(res => {
        LsetItem('jwtToken', res)
        report()
        next({
          path: '/',
          replace: true
        })
      })
    }

  }

})

router.afterEach((to) => {
  document.title = to.query.setTitle || to.meta.title
})

export const loadConfig = (next, to) => {
  api('config.getConfig', {
    voteId: LgetItem('voteId')
  }).then(res => {
    const {
      NAV,
      BASIC,
      AD,
      PAGE,
      NOTICE,
      RULE,
      SIGN_UP,
      WECHAT,
      SECURITY,
      COMMENTARY,
      SHARE
    } = res
    store.commit('configs/SET_BASIC', BASIC)
    store.commit('configs/SET_AD', AD)
    store.commit('configs/SET_PAGE', PAGE)
    store.commit('configs/SET_NOTICE', NOTICE)
    store.commit('configs/SET_RULE', RULE)
    store.commit('configs/SET_SIGN_UP', SIGN_UP)
    store.commit('configs/SET_WECHAT', WECHAT)
    store.commit('configs/SET_SECURITY', SECURITY)
    store.commit('configs/SET_COMMENTARY', COMMENTARY)
    store.commit('configs/SET_SHARE', SHARE)
    //格式化路由对象
    const asyncRouter = filterAsyncRouter(NAV.nav)
    //格式化tabbar
    store.dispatch('configs/setNav', NAV.nav).then(res => {
      console.log(res)
      asyncRouter.unshift({
        path: '/',
        meta: {
          title: '首页'
        },
        redirect: res[0].link
      })
      asyncRouter.push({
        path: '*',
        redirect: '/404'
      })
      console.log(asyncRouter)
      router.addRoutes(asyncRouter)
      // if (isIos()) {
      //   wxInit().then(() => {
      //     next({
      //       ...to,
      //       replace: true
      //     })
      //   })
      // } else {
      //   next({
      //     ...to,
      //     replace: true
      //   })
      // }
      next({
        ...to,
        replace: true
      })
      if (isIos()) {
        wxInit()
      }
    })
  })
}
export const report = () => {
  axios
    .get(`/vote/statistic/visit/report/${LgetItem("voteId")}`)
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });
}